package link.ten_fei.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import link.ten_fei.Bean.Doc_User;
import link.ten_fei.util.ConnectionFactory;
import link.ten_fei.util.MD5Util;

public class UserJdcDaoImpl implements UserJdcDao {

	@Override
	public List<Doc_User> find(String doc_name, int id) {
		// 1.获取数据库连接
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement preparedStatement = null;
		ResultSet rs = null;
		try {
			// 2.准备sql语句
			String sql = null;
			if ((doc_name == null || doc_name.equals("")) && id == 0) {
				// 限定搜索前100个
				sql = "select id,doc_name,doc_sex,doc_age,doc_title,doc_department,doc_tel from doc_user where doc_judge=1 limit 100";
			} else if (doc_name != null && !doc_name.equals("")) {
				sql = "select id,doc_name,doc_sex,doc_age,doc_title,doc_department,doc_tel from doc_user where doc_judge=1 and doc_name='"
						+ doc_name + "'";
			} else if ((doc_name == null || doc_name.equals("")) && id != 0) {
				sql = "select id,doc_name,doc_sex,doc_age,doc_title,doc_department,doc_tel from doc_user where id="
						+ id;
			}
			// 3.获取连接通路
			preparedStatement = connection.prepareStatement(sql);
			// 4.执行sql语句,返回查询的结果,封装在rs中
			rs = preparedStatement.executeQuery();
			// 5.将查询到的结果对象封装到一个集合中
			List<Doc_User> users = new ArrayList<Doc_User>();
			// 查询的结果包含表头信息
			while (rs.next()) {
				Doc_User user = new Doc_User();
				// 每一行的数据封装在一个实体bean中，根据字段获取字段值，该字段是什么类型就get什么类型
				user.setId(rs.getInt("id"));
				user.setDoc_name(rs.getString("doc_name"));
				user.setDoc_sex(rs.getString("doc_sex"));
				user.setDoc_age(rs.getString("doc_age"));
				user.setDoc_title(rs.getString("doc_title"));
				user.setDoc_department(rs.getString("doc_department"));
				user.setDoc_tel(rs.getString("doc_tel"));

				users.add(user);
			}
			return users;
		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(connection, preparedStatement, rs);
		}

		return null;
	}

	@Override
	public int deleteById(int id) {
		// 1.获取数据库连接
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement preparedStatement = null;
		int num = 0;
		try {
			// 2.键入sql语句
			String sql = "delete from doc_user where id=?";
			// 3.获取连接通路
			preparedStatement = connection.prepareStatement(sql);
			// 4.执行索引
			preparedStatement.setInt(1, id);
			// 5.更新到数据库
			num = preparedStatement.executeUpdate();
		} catch (SQLException e) {
			// TODO 自动生成的 catch 块
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(connection, preparedStatement, null);
		}
		return num;
	}

	@Override
	public void reset(int id) {
		// 1.获取数据库连接
		Connection connection = ConnectionFactory.getConnection();
		PreparedStatement preparedStatement = null;
		try {
			// 2. 键入sql语句
			String sql = "update doc_user set doc_username=(select docuser.doc_name from (select doc_name from doc_user where id=?)docuser),doc_password=? where id=?";
			// 3. 获取连接通路
			preparedStatement = connection.prepareStatement(sql);
			// 4. 占位符赋值
			preparedStatement.setInt(1, id);
			preparedStatement.setString(2, MD5Util.encodeByMD5("123456"));
			preparedStatement.setInt(3, id);
			// 5. 执行更新语句
			preparedStatement.executeUpdate();
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			ConnectionFactory.close(connection, preparedStatement, null);
		}
	}
}
